package services;

import dao.DateReport_Repository;
import models.*;
import play.db.jpa.JPAApi;

import javax.inject.Inject;
import java.util.List;
import java.util.concurrent.CompletionStage;

import static java.util.concurrent.CompletableFuture.supplyAsync;

public class DateReport_Repository_JPA implements DateReport_Repository{
    private final JPAApi jpaApi;
    private final DatabaseExecutionContext executionContext;

    @Inject
    public DateReport_Repository_JPA(JPAApi jpaApi, DatabaseExecutionContext executionContext) {
        this.jpaApi = jpaApi;
        this.executionContext = executionContext;
    }

    @Override
    public CompletionStage<List<DateReport_P_Entity>> getDateReport (String StartDate, String EndDate, String HWMC, String KHMC)
    {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
//                String strSql = "exec p_CreateDateReport @PhoneNumber = '"+PhoneNumber+"',@StartDate = '" + StartDate + "',@EndDate = '"+ EndDate+"',@HWMC = '"+HWMC+"',@KHMC ='"+KHMC+"',@MonthFlag ='"+MonthFlag+"' ";
                String strSql = "SELECT * FROM dbo.f_GetReport('"+StartDate+"','"+EndDate+"','"+HWMC+"','"+KHMC+"')";
                return entityManager.createNativeQuery(strSql, DateReport_P_Entity.class).getResultList();


            });
        }, executionContext);
    }

    @Override
    public CompletionStage<List<DateReportDept_P_Entity>> getReportDept (String StartDate, String EndDate, String HWMC)
    {
        return supplyAsync(() -> {
            return jpaApi.withTransaction(entityManager -> {
//                String strSql = "exec p_CreateDateReport @PhoneNumber = '"+PhoneNumber+"',@StartDate = '" + StartDate + "',@EndDate = '"+ EndDate+"',@HWMC = '"+HWMC+"',@KHMC ='"+KHMC+"',@MonthFlag ='"+MonthFlag+"' ";
                String strSql = "SELECT * FROM dbo.f_GetReportDept('"+StartDate+"','"+EndDate+"','"+HWMC+"')";
                return entityManager.createNativeQuery(strSql, DateReportDept_P_Entity.class).getResultList();


            });
        }, executionContext);
    }
}
